Technote 1089HFS Elucidations RevisitedBy Bo3b JohnsonRevised By Laura Rawson Revised By Quinn “The Eskimo!” Apple Worldwide Developer Technical Support |
CONTENTSOne Close is Always
Enough |
This Technote describes a few problems that may occur while using Hierarchical File System (HFS). It also describes ways to avoid these problems. This Note is important for developers who need to address debugging issues involving HFS. This is as important now as it was when the Note was originally published. The Note discusses the following problems:
Each of these can lead to strange occurrences, as well as problems for users. Performing any or all of these marginally illegal operations will not necessarily lead to a System Error. In some cases, the confusion generated may be worse than a System Error. |
One Close is Always EnoughIf a file is closed twice, it is possible to corrupt the file system on a disk. Without a clear understanding of how the file system allocates access paths to files that are currently open, it is possible to adopt a rather cavalier attitude about opening and closing files. This Note explains why it is necessary to be very careful about opening and closing files. When the File Manager receives an
A call such as:
will create the access path as a buffer and a File Control Block (FCB) in the FCB buffer. The term “FCB buffer” is used in most documentation, although it actually behaves more like an array than a buffer. However, to avoid confusion, this Technote will continue to use the term “FCB buffer,” although “FCB array” would be a better description.
The
Figure 1As depicted, any given record can be found by adding the
length of the previous FCB records to the start of the
block, adding 2 for the two-byte header; thus, giving an offset to the record itself. The size of the block, and hence the
number of files that can be open at any given time, is
determined at startup time and expanded on demand later. The
call to open the file referenced by A visual example of a record in use, and how the
Figure 2
Let’s step through a dangerous imaginary sequence and see what happens to a given record in the FCB buffer. Here’s the sequence we will step through:
Before any operations, the record at
Figure 3After the call:
Figure 4After the call:
Figure 5After the call:
Figure 6After the call:
The Figure 7
There are any number of nasty cases that can arise if a
file is closed twice, reusing an old As a particularly nasty example, think of what can happen if a program were to close a file, then the user inserted an HFS floppy disk. The FCB could be reused for the Catalog File on that HFS disk. If the program had a generic error handler that closed all of its files, it could inadvertently close “its” file again. If it thought “its” file was still open it would do the close, which could close the Catalog file on the HFS disk. This is catastrophic for the disk since the file could easily be closed in an inconsistent state. The result is a bad disk that needs to be reformatted. |
Avoiding the Problem of Overwriting The FCB RecordA very simple technique is to merely clear the
This makes the second
Thus, if an error handler were cleaning up possibly open
files, it could blithely close all the files it knew about,
since it would legitimately get an error back on files that
are already closed. This is not done automatically, however.
The programmer must be careful about the opening and closing
of files. The problem can get quite complex if an error is
received halfway through opening a sequence of ten files,
for example. By merely clearing the |
This .filename Looks OutrageousThere is a potential conflict between file names and
driver names when using deprecated
|
|
Thanks to Bo3b Johnson, Pete Gontier, Jim Luther, and Tom Maughan.